﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using System;
using System.Data;
using System.IO;
using System.Windows;
namespace WPFExcelReport
{

    public class MainViewModel : BaseFunction
    {
        public string dbConnection = "C:/IntexReport/Data/ITXPDC/YGR.s3db";
        //public SQLiteDatabase SQLiteDatabase;
        public bool OnLine = true;
        VICC_PDC PDC;
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public string TERMNO
        {
            get
            {
                return app.TERMNO;
            }
        }
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetProd
        {
            get;
            set;
        }
        public SETINTEXDB SetData
        {
            get;
            set;
        }
        private string m_LFD;
        public string LFD
        {
            get
            {
                return m_LFD;
            }
            set
            {
                m_LFD = value;
            }
        }
        private string m_KRS;
        public string KRS
        {
            get
            {
                return m_KRS;
            }
            set
            {
                m_KRS = value;
            }
        }
        private string m_POS;
        public string POS
        {
            get
            {
                return m_POS;
            }
            set
            {
                m_POS = value;
            }
        }
        private string m_STEP;
        public string STEP
        {
            get
            {
                return m_STEP;
            }
            set
            {
                m_STEP = value;
            }
        }
        public bool IsMaster
        {
            get
            {
                return app.IsMaster;
            }
        }
        private string m_MachineID;
        public string MACHINEID
        {
            get
            {
                return m_MachineID;
            }
            set
            {
                m_MachineID = value;
            }
        }
        private string m_AMBEZ;
        public string AMBEZ
        {
            get
            {
                return m_AMBEZ;
            }
            set
            {
                m_AMBEZ = value;
            }
        }
        private string m_AMBDEMA;
        public string AMBDEMA
        {
            get
            {
                return m_AMBDEMA;
            }
            set
            {
                m_AMBDEMA = value;
            }
        }

        public MainViewModel()
            : base()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetData == null)
            {
                SetData = new SETINTEXDB();
            }
            if (SetProd == null)
            {
                SetProd = new SETINTEXDB();
            }
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
            CheckDirectory();
        }
        ~MainViewModel()
        {
            if (exAgent != null)
            {
                exAgent.Close();
                exAgent.Dispose();
                exAgent.KillExcelProcess();
                GC.Collect();
                exAgent = null;
            }
        }
        private void CheckDirectory()
        {
            string sql = "";
            if (!System.IO.Directory.Exists(@"C:\IntexReport"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport");
            }
            if (!System.IO.Directory.Exists(@"C:\IntexReport\Data"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport\Data");
            }
            if (!System.IO.Directory.Exists(@"C:\IntexReport\Data\ITXPDC"))
            {
                System.IO.Directory.CreateDirectory(@"C:\IntexReport\Data\ITXPDC");
            }
            return;
            /*
         
            if (File.Exists(dbConnection))
            {
                SQLiteDatabase = new SQLiteDatabase(dbConnection);
                string edate = Helpers.Helper.GetDateAddDays(date, -60);
                sql = "DELETE FROM ICC_PDC WHERE NEWDATE<'" + edate + "'";
                SQLiteDatabase.ExecuteNonQuery(sql);
                return;
            }
            if (!File.Exists(SOURCE))
            {
                MessageBox.Show("请检查文件" + app.company + ".s3db");
                return;
            }
            return;
            string SOURCE = app.ApplicationPath + "/Report/YGR/YGR.s3db";
            if (!File.Exists(SOURCE))
            {
                SOURCE = app.ApplicationPath + "/YGR.s3db";
                if (!File.Exists(SOURCE))
                {
                    MessageBox.Show("请检查文件" + app.company + ".s3db");
                    return;
                }
            }
            File.Copy(SOURCE, dbConnection);
            SQLiteDatabase = new SQLiteDatabase(dbConnection);
            string NOrder = "1";
            DataRow dwData = SetEnv.ICC_PDC.NewRow();
            dwData["TERMNO"] = app.TERMNO;
            DataSet SResult = INTEXDBC.QueryICC_PDC(dwData, 3);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string max = row["MAXORD"].ToString();
                if (max.Trim() == "")
                    break;
                NOrder = max.Replace(app.TERMNO.Trim() + ".", "");
            }
            sql = "UPDATE ICC_MAXORD SET MAXORD= '" + NOrder + "'";
            SQLiteDatabase.ExecuteNonQuery(sql);
            sql = "DELETE FROM ICC_PDC";
            SQLiteDatabase.ExecuteNonQuery(sql);
             * */
        }
    }
}
